package com.jlastu.znb.view;



import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jlastu.znb.R;

public class SchoolFragment extends Fragment {

    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    public SchoolFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_school, container, false);

        viewPager = view.findViewById(R.id.view_pager);
        tabLayout = view.findViewById(R.id.tab_layout);

        SchoolPagerAdapter pagerAdapter = new SchoolPagerAdapter(this);
        viewPager.setAdapter(pagerAdapter);

        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> tab.setText(pagerAdapter.getPageTitle(position))
        ).attach();

        return view;
    }

    public class SchoolPagerAdapter extends FragmentStateAdapter {
        public SchoolPagerAdapter(Fragment fragment) {
            super(fragment);
        }


        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    return new LessonFragment();
                case 1:
                    return new VideoFragment();
                case 2:
                    return new ArticleFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getItemCount() {
            return 3;
        }



        public CharSequence getPageTitle(int position) {
            // 根据位置提供不同的标题
            switch (position) {
                case 0:
                    return "热点新闻";
                case 1:
                    return "专家咨询";
                case 2:
                    return "创业项目";
                default:
                    return null;
            }
        }
    }
}